package com.whz.mlc.provider.loader;

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.whz.mlc.cache.second.RemoteCache;
import com.whz.mlc.cache.second.redis.MlcEmptyValue;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * 关联二级缓存的本地缓存{@link CacheLoader}
 *
 * @author whz
 *  2021/8/17
 */
@Slf4j
public class LocalCacheLoader implements CacheLoader<Object, Object> {

    private final RemoteCache remoteCache;

    public LocalCacheLoader(RemoteCache remoteCache) {
        this.remoteCache = remoteCache;
    }

    @Override
    public @Nullable Object load(@NonNull Object key) {
        try {
            return remoteCache.get(String.valueOf(key));
        } catch (Exception exception) {
            log.error("local cache catch error.", exception);
            return MlcEmptyValue.INSTANCE;
        }
    }
}